package Base;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;

/**
 * Created by Alain on 2016/3/12.
 */
@SuppressWarnings("unused")
public class DataFactory{
    private static String filePath = PropertyUtil.getInitialData("Base.filePath");
    private static String hostPath = PropertyUtil.getInitialData("Base.hostPath");
    private String sheetName = getHost(hostPath);
    private static HSSFSheet mySheet;
    private FileInputStream FIS;
    private static HSSFWorkbook  myDataFactory;
    private String cellValue=null;
    private static HSSFRow currentRow;

    //get cell value
    public String getCellValue(String columnName, String rowName){
        try{
            URL fileUrl = this.getClass().getClassLoader().getResource(filePath);
            FIS = new FileInputStream(fileUrl.getPath());
            myDataFactory = new HSSFWorkbook(FIS);
            mySheet = myDataFactory.getSheet(sheetName);

            currentRow = mySheet.getRow(getRowIndex(mySheet, rowName));

            switch (currentRow.getCell(getColumnIndex(mySheet, columnName)).getCellType()){
                case 0:
                    return String.valueOf(BigDecimal.valueOf(currentRow.getCell(getColumnIndex(mySheet, columnName)).getNumericCellValue()));
                case 1:
                    return currentRow.getCell(getColumnIndex(mySheet, columnName)).getStringCellValue();
                default:
                    return null;
            }

        } catch (Exception e){}

        return cellValue;
    }

    //get row index
    private int getRowIndex(HSSFSheet mySheet, String rowName){
        int rowIndex = 0;
        try{
            for(int i=0; i<mySheet.getLastRowNum(); i++){
                if (mySheet.getRow(i).getCell(0) != null){
                    String cellValue = mySheet.getRow(i).getCell(0).toString();
                    if (cellValue.trim().equalsIgnoreCase(rowName)){
                        rowIndex = i;
                        break;
                    }
                }
            }
        } catch (Exception e){}
        return rowIndex;
    }

    //get column index
    private int getColumnIndex(HSSFSheet mySheet, String columnName){
        int columnIndex = 0;
        try{
            for(int i=0; i<mySheet.getRow(0).getLastCellNum(); i++){
                if (mySheet.getRow(0).getCell(i) != null){
                    String cellValue = mySheet.getRow(0).getCell(i).toString();
                    if (cellValue.trim().equalsIgnoreCase(columnName)){
                        columnIndex = i;
                        break;
                    }
                }
            }
        } catch (Exception e){}
        return columnIndex;
    }

    //get environment host
    private String getHost(String hostPath){
        File hostFile = new File(hostPath);
        String environment = null;
        String hostContent;
        try{
            BufferedReader BR = new BufferedReader(new InputStreamReader(new FileInputStream(hostFile), "utf-8"));

            while ((hostContent = BR.readLine()) != null){
                if (!hostContent.startsWith("#") && hostContent.contains("139.217.18.33")){
                    environment = "Test";
                    break;
                }

                environment = "Online";
            }
        }catch (Exception e){}

        return environment;
    }
}
